import {
    View, ScrollView, Dimensions, RefreshControl, TouchableOpacity,
    TouchableHighlight, Text, StyleSheet, Image, WebView, Linking
} from 'react-native';
import React, {Component} from 'react';
import Urls, {getRealUrl} from '../const/Urls'
import LocalImg from '../const/Images'
import moment from 'moment'
import {Toast} from 'antd-mobile-rn'

const {width, height} = Dimensions.get("window");

class PageZyggDetail extends Component {

    constructor(props) {
        super(props);
        this.model = this.props.navigation.getParam('model');
    }


    processDateTime(date) {
        const m = moment(date)
        if (m.isValid()) {
            return m.format('DD/HHmm')
        } else
            return ""
    }

    render() {
        const model = this.model;


        let VCCONTAIN = model.VCCONTAIN.replace('/>', '/>\r\n');
        let pett = new RegExp("<img.*(?=>)(.|\n)*?>");
        VCCONTAIN = VCCONTAIN.replace(pett, function (str) {
            var src;
            if (str.indexOf('202.136.214.163:801') === -1 && str.indexOf('data:') === -1) {
                src = str.slice(str.indexOf('src') + 5, +(str.indexOf('>') - 4) + 1 || 9e9);
                src = "http://202.136.214.163:801" + src;
                return "<img alt='' src='" + src + "' />";
            } else {
                return str;
            }
        });

        return (

            <View style={{width: width, height: height, flexDirection: 'column', backgroundColor: '#ffffff'}}>
                <View style={{
                    flexDirection: 'row',
                    flex: 0,
                    height: 60,
                    alignItems: 'center',
                    justifyContent: 'center',
                    backgroundColor: '#0066FF'
                }}>
                    <View style={{flex: 1, padding: 10}}>
                        <TouchableOpacity onPress={() => this.props.navigation.pop()}>
                            <Image source={LocalImg['backImg']} style={{height: 30, width: 30, resizeMode: 'contain'}}/>
                        </TouchableOpacity>
                    </View>
                    <View style={{flex: 3, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{fontSize: 20, color: '#ffffff'}}>详情</Text>
                    </View>
                    <View style={{flex: 1, alignItems: 'flex-end', padding: 10}}>
                    </View>
                </View>
                <View style={{flexDirection: 'column', flex: 1, marginTop: 10, backgroundColor: '#ffffff'}}>
                    <View style={styles.row}>
                        <View style={styles.cell}>
                            <Text style={[styles.text, {
                                fontSize: 20,
                                fontWeight: 'bold',
                                textAlign: 'center'
                            }]}>{model.VCTITLE}</Text>
                        </View>
                    </View>
                    <View style={styles.row}>
                        <View style={styles.cell}>
                            <Text style={[styles.text, {textAlign: 'right'}]}>{model.DTTIME}</Text>
                        </View>
                    </View>
                    <View style={[styles.row, {flex: 10, marginBottom: 40}]}>
                        <WebView source={{
                            html: VCCONTAIN, baseUrl: ''
                        }}
                                 canGoForward={true}
                                 style={{margin: 5}}
                                 javaScriptEnabled={true}
                        >
                        </WebView>


                    </View>
                    {
                        model.VCCONTAIN1 ?
                            <View style={{flex: 1, marginBottom: 20}}>
                                <TouchableOpacity onPress={() => this.downloadFile()}>
                                    <View style={{paddingLeft: 10, paddingRight: 10}}>
                                        <Text style={{color: 'blue'}}>【请点击下载文件】</Text>
                                    </View>
                                </TouchableOpacity>
                            </View>
                            : <View/>
                    }
                </View>
            </View>

        )
    }

    downloadFile() {
        const url = Urls['downloadUrl'] + this.model.VCCONTAIN1;
        return Linking.canOpenURL(url).then(function (supported) {
            if (supported) {
                return Linking.openURL(url);
            } else {
                return Toast.show("不能识别当前网址");
            }
        });
    }


}

const styles = StyleSheet.create({
    row: {
        flexDirection: 'row',
        borderWidth: 1,
        borderStyle: 'solid',
        borderColor: '#CCCCCC',
        backgroundColor: '#ffffff',
        padding: 10,
        marginLeft: 10,
        marginRight: 10,
    },

    cell: {
        flex: 1,

        flexDirection: 'row',
    },
    text:
        {
            flex: 1
        }
});


export default PageZyggDetail;